import GameController from "./game_controller";
 
 

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class Cell extends cc.Component {


    @property(cc.Label)
    valueLabel:cc.Label=null;

    _value:number=1;

    get value() {
        return this._value;
    }

    set value(value: number) {
        this._value = value;
        this.valueLabel.string=value+"";
        let spriteValue=value;
        if(spriteValue>=2018){
            spriteValue=2018;
        }
        this.sprite.spriteFrame = GameController.getInst().cellGroup.getCellSprite(value);
        this.motionStreak.color= GameController.getInst().cellGroup.getCellColorByValue(value);
    }


    colRowPos:cc.Vec2=null;

    @property(cc.Sprite)
    sprite:cc.Sprite=null;
 
    motionStreak:cc.MotionStreak=null;

    
    onLoad () {
        this.motionStreak=this.node.getComponent(cc.MotionStreak);
        this.node.on(cc.Node.EventType.TOUCH_START,this.touchStart,this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL,this.touchCancel,this);
        this.node.on(cc.Node.EventType.TOUCH_END,this.touchCancel,this);
     }

       
   
   touchStart(t){
       GameController.getInst().cellGroup.select(this.node);
  
  
   }

   touchCancel(){
       
   }

 
   
   onDestroy(){
    this.node.off(cc.Node.EventType.TOUCH_START,this.touchStart);
     this.node.off(cc.Node.EventType.TOUCH_CANCEL,this.touchCancel);
     this.node.off(cc.Node.EventType.TOUCH_END,this.touchCancel);
}


    // LIFE-CYCLE CALLBACKS:

    init(value){
        this.value=value;
    }

    onClick(){

    }
 

    start () {

    }

    // update (dt) {}
}
